package com.itinsys.enma.web;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.Shift;
import com.itinsys.enma.domain.response.ShiftResponse;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.ShiftManager;

@Controller
@RequestMapping({ "/shift" })
public class ShiftFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");
	
	@Autowired
	private ShiftManager shiftManager;

	@Autowired
	private ResponseManager responseManager;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public ShiftResponse getAllShift() {
		return shiftManager.getAllShifts();
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ShiftResponse addShift(@RequestBody Map<String,String> shiftDTO,HttpServletResponse response) {
		Shift shift = dtoToShift(shiftDTO);
		return this.shiftManager.addShift(shift);
	}

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ShiftResponse editShift(@RequestBody Map<String,String> shiftDTO,HttpServletResponse response) {
		Shift shift = dtoToShift(shiftDTO);
		return this.shiftManager.updateShift(shift);
	}
	

	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ShiftResponse deleteShift(@RequestBody Map<String,String> shiftDTO,HttpServletResponse response) {
		Shift shift = new Shift();
		shift.setId(Integer.parseInt(shiftDTO.get("id")));
		return this.shiftManager.deleteShift(shift);
	}
	
	/**
	 * @param shiftDTO
	 * @return
	 */
	private Shift dtoToShift(Map<String, String> shiftDTO) {
		Shift shift=new Shift();
		SimpleDateFormat f = new SimpleDateFormat("HH:mm");
		GregorianCalendar tmpDate = new GregorianCalendar();
		try {
			tmpDate.setTime(f.parse(shiftDTO.get("startTime")));
		
			Time startHour=new Time(tmpDate.getTime().getTime());
			tmpDate.setTime(f.parse(shiftDTO.get("endTime")));
			Time endHour=new Time(tmpDate.getTime().getTime());
			if(shiftDTO.get("shiftId")!=null)
				shift.setId(Integer.parseInt(shiftDTO.get("shiftId")));
			shift.setDescription(shiftDTO.get("shiftName"));
			shift.setStartTime(startHour);
			shift.setEndTime(endHour);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return shift;
	}

	
}
